#include "disjoint_set.h"

mypointsto::disjoint_set::disjoint_set(){
	size = default_size;
	p = new int [default_size];
	memset(p, 0xff, size*sizeof(int));
}

mypointsto::disjoint_set::disjoint_set(int sz):size(sz){
	p = new int [size];
	memset(p, 0xff, size*sizeof(int));
}

void mypointsto::disjoint_set::resize(int sz){
	if(!p || sz<=size)
		return;
	int *q = new int [size];
	for(int i=0; i<size; i++)
		q[i] = p[i];
	delete [] p;
	p = new int [sz];
	memset(p, 0xff, sz*sizeof(int));
	for(int i=0; i<size; i++)
		p[i] = q[i];
	size = sz;
	delete [] q;
}

mypointsto::disjoint_set::~disjoint_set(){
	if(p)
		delete p;
}

int mypointsto::disjoint_set::find(int i){
	if(p[i]>=size){
		std::cout<<i<<" "<<size<<std::endl;
	}
	if(p[i]<0)return i;
	else
		return (p[i]=find(p[i]));
}

void mypointsto::disjoint_set::merge(int i, int j){
	int x = find(i);
	int y = find(j);
    if(x==y)return;
	p[x] += p[y];
	p[y] = x;
}
